<#assign object = doc.objects>
package ${object.package}.service;

import java.io.PrintWriter;

public class ModelViewService {

	public static void getModelNames(PrintWriter out) {
		out.print('[');
		<#list object["/objects/object/name"] as modelname>
		out.print('{');
		out.print("\"objectName\":");
		out.print('\"');
		out.print("${modelname}");
		out.print('\"');
		out.print('}');
		<#if modelname_has_next>
		out.print(',');
		</#if>
		</#list>
		out.print(']');
	}
	
	public static void getModelFields(String modelName, PrintWriter out){
		out.print("\"field\":[");
		<#list object["/objects/object"] as model>
		if(modelName.equalsIgnoreCase("${model.name}")){
			<#assign n = 0 />
				<#list model.attributes?children as att>
					<#if att?node_type == "element">
						<#if att?node_name == "long" && att.@parentClass[0]??>
						<#else>
						<#assign n = n+1 />
			<#if n gt 1>
			out.print(',');
			</#if>
			//add field_name
			out.print("{\"field_name\":\"");
						<#if att?node_name != "set" && att?node_name != "object">
			out.print("${att.@name[0]!att.@class?uncap_first}");
						<#elseif att?node_name == "set">
			out.print("${att.@name[0]!att.@class?uncap_first}s");
						<#elseif att?node_name == "object">
			out.print("${att.@name[0]!att.@class?uncap_first}");
						</#if>
			out.print("\",");
				
			//add field_type 	
			out.print("\"field_type\":\"");
			out.print("${att?node_name}");
			out.print("\",");
			
			//add not_null attribute	
			out.print("\"not_null\":");
			out.print('\"');
						<#if att.@notnull[0]??>
							<#if att.@notnull[0] == "true">
			out.print("true");
							<#else>
			out.print("false");
							</#if>
						<#else>
			out.print("false");
						</#if>
			out.print("\",");
			
			//add default attribute
						<#if att.@default[0]??>
			out.print("\"default\":");
			out.print('\"');
			out.print("${att.@default}");
			out.print("\",");
						</#if>
			
			//add regexp attribute
						<#if att.@regexp[0]??>
			out.print("\"regexp\":");
			out.print('\"');
			out.print("${att.@regexp}");
			out.print("\",");
						</#if>
			
			//add maxlength attribute				
						<#if att.@maxlength[0]??>
			out.print("\"maxlength\":");
			out.print('\"');
			out.print("${att.@maxlength}");
			out.print("\",");
						</#if>
			
			//add finder attribute	
			out.print("\"finder\":");
			out.print('\"');
						<#if att.@finder[0]??>
							<#if att.@finder[0] == "true">
			out.print("true");
							<#else>
			out.print("false");
							</#if>
						<#else>
			out.print("false");
						</#if>
			out.print("\",");
			
			//add lazy attribute
			out.print("\"lazy\":");
			out.print('\"');
						<#if att.@lazy[0]??>
							<#if att.@lazy[0] == "true">
			out.print("true");
							<#else>
			out.print("false");
							</#if>
						<#else>
			out.print("false");
						</#if>
			out.print("\",");
			
			//add list attribute
			out.print("\"list\":");
			out.print('\"');
						<#if att.@list[0]??>
							<#if att.@list[0] == "true">
			out.print("true");
							<#else>
			out.print("false");
							</#if>
						<#else>
			out.print("false");
						</#if>
			out.print("\"}");
					</#if>
				</#if>
				</#list>
			}
		</#list>	
		out.print("],");
		
		out.print("\"businessRule\":[");
		<#list object["/objects/object"] as model>
		if(modelName.equalsIgnoreCase("${model.name}")){
			<#if model.businessRules[0]??>
			<#list model.businessRules.businessRule as att>
			out.print("{number:");
			out.print("\"${att.@number}\"");
			out.print(',');
			out.print("javadoc:");
			out.print("\"${att.@javadoc}\"");
			out.print(",attributes:");
				<#if att.@attributes[0]??>
			out.print("\"${att.@attributes}\"");
				<#else>
			out.print("null");	
				</#if>
			out.print('}');
				<#if att_has_next>
			out.print(',');
				</#if>
			</#list>
			</#if>
		}
		</#list>
		out.print(']');
	}
	
	public static void getFindFields(String modelName, PrintWriter out){
		out.print('[');
		<#list object["/objects/object"] as model>
		if(modelName.equalsIgnoreCase("${model.name}")){
			<#assign n = 0 />
			<#list model.attributes?children as att>
				<#if att.@finder[0]?? && att.@finder[0] == "true">
					<#assign n = n+1 />
					<#if n gt 1>
					out.print(',');
					</#if>
					out.print('{');
					out.print("\"finder_field_name\":");
					out.print("\"${att.@name}\"");
					out.print(',');
					out.print("\"finder_field_method\":");
					out.print("\"findBy${att.@name?cap_first}\"");
					out.print('}');
				</#if>
			</#list>
		}
		</#list>
		out.print(']');
	}
}
